package com.example.service;

import com.example.entity.Setmeal;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.setmealGroup;
import com.example.mapper.SetmealMapper;
import com.example.mapper.setmealGroupMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class SetmealService extends ServiceImpl<SetmealMapper, Setmeal> {

    @Resource
    private SetmealMapper setmealMapper;
    @Resource
    private setmealGroupMapper setmealGroupMapper; // 中间表的持久层

    @Transactional
    public void addSetmealSave(String itemGroupids, Setmeal setmeal) {
        setmealMapper.insert(setmeal); //套餐的增加
        String [] split = itemGroupids.split(",");
        for (String id : split) {
            setmealGroup setmealGroup = new setmealGroup();
            setmealGroup.setGroupId(Integer.parseInt(id));
            setmealGroup.setSetmealId(Integer.valueOf(setmeal.getId().toString()));
            setmealGroupMapper.insert(setmealGroup);//中间表的循环增加
        }
    }

    public Setmeal setmealMore(Long id){
        Setmeal setmeal = setmealMapper.setmealMore(id);
        return setmeal;
    }
}